package cn.cimoc.service.impl;

import cn.cimoc.mapper.CartMapper;
import cn.cimoc.mapper.ProductMapper;
import cn.cimoc.model.dto.inut.CartInput;
import cn.cimoc.model.dto.output.CartDTO;
import cn.cimoc.model.po.TCart;
import cn.cimoc.model.po.TProduct;
import cn.cimoc.service.CartService;
import cn.cimoc.util.CommonUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author LGZ
 * @date 2022/7/16 14:22
 * <p>
 */
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    ProductMapper pMapper;

    @Autowired
    CartMapper mapper;

    @Override
    public void addToCart(long pid, long amount, long uid, String username) {
        TCart tCart = new TCart();
        tCart.setPid(pid);
        tCart.setNum(amount);
        tCart.setCreatedUser(username);
        tCart.setUid(uid);
        tCart.setModifiedUser(username);

        TProduct tProduct = pMapper.selectById(pid);
        tCart.setPrice(amount * tProduct.getPrice());

        mapper.insert(tCart);
    }

    @Override
    public CartDTO[] getCartList(long uid) {
        return mapper.getCartList(uid);
    }

    @Override
    public CartDTO[] getCartByCid(Long[] cids) {
        return mapper.getCartByCid(cids);
    }
}
